package com.shycloud.mido.common.security.service;

import cn.hutool.core.util.StrUtil;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.ArrayList;
import java.util.List;

/**
 * @author nianhua.jiang
 * @description
 * @date 2020/6/12
 */
public class AuthorityUtilsExt extends AuthorityUtils {


	public static List<GrantedAuthority> createAuthorityListExt(String flg, String... roles) {

		List<GrantedAuthority> authorities = new ArrayList<>(roles.length);

		for (String role : roles) {

			if ("opt".equals(flg) || "org".equals(flg)) {
				if (!StrUtil.startWithAny(role, "ROLE_")) {
					authorities.add(new SimpleGrantedAuthority(role));
				}
				continue;
			}
			authorities.add(new SimpleGrantedAuthority(role));
		}

		if ("opt".equals(flg)) {
			authorities.add(new SimpleGrantedAuthority("ROLE_18"));
		}

		if ("org".equals(flg)) {
			authorities.add(new SimpleGrantedAuthority("ROLE_19"));
		}

		return authorities;
	}

}
